﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;

namespace FineUI.Examples
{
    public partial class teacher_rate_report : System.Web.UI.Page
    {

        protected void DocDownLoad(string current_RS_Id,string current_T_Id,string current_S_Id)
        {

            Maticsoft.BLL.ReportSubmit documentation = new Maticsoft.BLL.ReportSubmit();
            Maticsoft.Model.ReportSubmit documentInform = documentation.GetModel(current_RS_Id, current_T_Id, current_S_Id);
            string fileRpath = Server.MapPath(@"~/document/student/") + documentInform.RS_Path;

            Response.ClearHeaders();
            Response.Clear();
            Response.Expires = 0;
            Response.Buffer = true;
            Response.AddHeader("Accept-Language", "zh-tw");
            string name = documentInform.RS_Formerly.ToString();
            System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] byteFile = new byte[files.Length];

            files.Read(byteFile, 0, byteFile.Length);
            files.Close();
            Response.ContentType = "application/octet-stream;charset=gbk";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));

            Response.BinaryWrite(byteFile);
            Response.Flush();
            Response.End();


        }
        protected DataSet LoadStuDocment()
        {
            string currentT_Id = Session["UserName"].ToString();
            Maticsoft.BLL.Teacher Teacher = new Maticsoft.BLL.Teacher();
            DataSet DSStudentCourseInform = Teacher.GetReportSubmitInformation("ReportSubmit.T_Id=Teacher.T_Id and ReportSubmit.S_Id=Student.S_Id and Teacher.T_Id= '" + currentT_Id + "'");

            DataView DSView = DSStudentCourseInform.Tables[0].DefaultView;
            Grid1.DataSource = DSView;
            Grid1.DataBind();

            return DSStudentCourseInform;
        
        }


        protected void ExistsPermiss(string P_Id, string P_Status)
        {
            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }
            ExistsPermiss("T004", "已授权");
            LoadStuDocment();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadStuDocment();

        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionDownLoad")
            {
                int index = e.RowIndex;
                DataSet currentDataSet = LoadStuDocment();
               
                string currentRS_Id = currentDataSet.Tables[0].Rows[index]["RS_Id"].ToString();
                string currentT_Id = currentDataSet.Tables[0].Rows[index]["T_Id"].ToString();
                string currentS_Id = currentDataSet.Tables[0].Rows[index]["S_Id"].ToString();
                DocDownLoad(currentRS_Id, currentT_Id, currentS_Id);
            }
            if (e.CommandName == "ActionPreview")
            {
                int index = e.RowIndex;
                DataSet currentDataSet = LoadStuDocment();

                string currentRS_Id = currentDataSet.Tables[0].Rows[index]["RS_Id"].ToString();
                string currentT_Id = currentDataSet.Tables[0].Rows[index]["T_Id"].ToString();
                string currentS_Id = currentDataSet.Tables[0].Rows[index]["S_Id"].ToString();


                Maticsoft.BLL.ReportSubmit documentation = new Maticsoft.BLL.ReportSubmit();
                Maticsoft.Model.ReportSubmit documentInform = documentation.GetModel(currentRS_Id, currentT_Id, currentS_Id);
                string fileRpath = Server.MapPath(@"~/document/student/") + documentInform.RS_Path;

                Session["Doc_Path"] = fileRpath;
                Session["Doc_Flag"] = "student";
                Response.Redirect("~/user_view_document.aspx");

            }



        }

        protected void Grid1_RowClick(object sender, GridRowClickEventArgs e)
        {
         

            int index=e.RowIndex;
            Session["current_RS_Grid_index"] = index;
            DataSet currentDataSet = LoadStuDocment();
            string courseName = currentDataSet.Tables[0].Rows[index]["RS_Correcting"].ToString();
            string uploadTime = currentDataSet.Tables[0].Rows[index]["RS_Date"].ToString();
            string studentName = currentDataSet.Tables[0].Rows[index]["S_Name"].ToString();
            string studentSocre = currentDataSet.Tables[0].Rows[index]["RS_Socre"].ToString();
            string studentAsk = currentDataSet.Tables[0].Rows[index]["RS_Ask"].ToString();
            string teacherAns = currentDataSet.Tables[0].Rows[index]["RS_Ans"].ToString();

            lableCourseName.Text = courseName;
            labelUploadTime.Text = uploadTime;

            tbxStuName.Text = studentName;
            tbxStuScore.Text = studentSocre;

            txareaQuestion.Text = studentAsk;
            txareaAnwser.Text = teacherAns;
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            if (tbxStuName.Text=="")
            {
                Alert.Show("请选中一行", MessageBoxIcon.Information);
                return;
            }

            int index= Convert.ToInt32(Session["current_RS_Grid_index"].ToString());
            DataSet currentDataSet = LoadStuDocment();

            string currentRS_Id = currentDataSet.Tables[0].Rows[index]["RS_Id"].ToString();
            string currentT_Id = currentDataSet.Tables[0].Rows[index]["T_Id"].ToString();
            string currentS_Id = currentDataSet.Tables[0].Rows[index]["S_Id"].ToString();

            Maticsoft.BLL.ReportSubmit documentStudent = new Maticsoft.BLL.ReportSubmit();
            Maticsoft.Model.ReportSubmit documentStudentInfrom = documentStudent.GetModel(currentRS_Id, currentT_Id, currentS_Id);
            documentStudentInfrom.RS_Socre = tbxStuScore.Text.ToString();
            documentStudentInfrom.RS_Ans = txareaAnwser.Text.ToString();

           if(documentStudent.Update(documentStudentInfrom))
           {
               Alert.Show("评分成功", MessageBoxIcon.Information);
               LoadStuDocment();
           }


        }


    }
}